﻿''' <summary>
''' 每月每日來電統計表
''' </summary>
''' <remarks>
''' Author:huyang
''' Create Date:2008-05-28
''' </remarks>
Public Class Frm_CSPrintMonthCount

    Implements IDServer.IDServer.IDServer

#Region "Variabled"

#Region "System Variabled Define"


    Public ActionHelp As IDServer.IDServer.ActionBtn = IDServer.IDServer.ActionBtn.ActionHelp

    Private WithEvents MainForm As Frm_MainForm

    Public Property SelectObject() As Frm_MainForm
        Get
            Return MainForm
        End Get
        Set(ByVal value As Frm_MainForm)
            MainForm = value
        End Set
    End Property

    Public action_help As IDServer.IDServer.ActionBtn = IDServer.IDServer.ActionBtn.ActionHelp

#End Region

#Region "user variabled define"

    ''' <summary>
    ''' 窗體是否加載
    ''' </summary>
    ''' <remarks></remarks>
    Private fLoadBool As Boolean = False

 
#End Region

#End Region

#Region "Methods"

#Region "ExecFind"

    Private Sub ExecFind()
        Me.Cursor = Cursors.WaitCursor

        Dim sql As String = ""
        Dim Dtab As New DataTable

        sql = "  Exec usp_ReportCSMonth   '" & Format(Me.dtp_start_1.Value, "yyyy-MM-dd") & "','" & Format(Me.dtp_end_1.Value, "yyyy-MM-dd") & "' "

        Try

            Dim fDays As Integer = 26
       
            Dtab = Exec(sql, E_ExecType.DataTable)

            Dtab.Columns.Add("fTotals", GetType(System.Double))
            Dtab.Columns.Add("fDays", GetType(System.Int32))
            Dtab.Columns.Add("fAverage", GetType(System.Double))

            ComputeDataTable(Dtab)

            Dtab.Columns("Person").ColumnName = "姓 名 "
            Dtab.Columns("fTotals").ColumnName = "來電總數"
            Dtab.Columns("fDays").ColumnName = "總天數"
            Dtab.Columns("fAverage").ColumnName = "平均每天處理來電總數"
            Dtab.AcceptChanges()


            Me.DataGridView1.Columns.Clear()
            Me.DataGridView1.DataSource = Dtab
            FullFormatDataGridView(Me.DataGridView1, False, False)
            Me.DataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect
            Me.DataGridView1.AutoResizeColumns(System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells)
            If Me.fLoadBool = True Then
                SetButton(ButtonType.Browse, False)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            Me.Cursor = Cursors.Default
        End Try

    End Sub

    Private Sub ComputeDataTable(ByRef Dtab As DataTable)

        For Each row As DataRow In Dtab.Rows
            Dim fTotal As Double = 0.0
            Dim fAverage As Double = 0.0
            Dim fDay As Integer = 0
            If Me.TabControl1.SelectedIndex = 0 Then
                fDay = Val(Me.TextBox1.Text)
                If fDay <= 0 Then
                    fDay = 26
                End If
            End If

            For Each col As DataColumn In Dtab.Columns
                fTotal += Val(row(col.ColumnName) & "")
                If Me.TabControl1.SelectedIndex = 1 Then
                    If Val(row(col.ColumnName) & "") > 0 Then
                        fDay += 1
                    End If
                End If
            Next
            row("fTotals") = fTotal
            row("fDays") = fDay
            If fDay > 0 Then
                row("fAverage") = (fTotal / fDay).ToString("0.0000")
            End If
        Next

    End Sub

    Private Sub ExecFind_2()
        Me.Cursor = Cursors.WaitCursor
        Dim sql As String = ""
        sql = "  Exec   usp_PrintComplainMonth '" & Format(Me.dtp_start_2.Value, "yyyy-MM-dd") & "','" & Format(Me.dtp_end_2.Value, "yyyy-MM-dd") & "','" & Me.cbxfLevel.Text.Trim & "' "
        Dim Dtab As New DataTable
        Try

            Dtab = Exec(sql, E_ExecType.DataTable)
            Dtab.Columns.Add("fTotals", GetType(System.Double))
            Dtab.Columns.Add("fDays", GetType(System.Int32))
            Dtab.Columns.Add("fAverage", GetType(System.Double))
            ComputeDataTable(Dtab)
            Dtab.AcceptChanges()

            Me.DataGridView2.Columns.Clear()
            Me.DataGridView2.DataSource = Dtab
            BindDataGridViewCaption_1(Me.DataGridView2, Me.ReturnCaption(1))
            FullFormatDataGridView(Me.DataGridView2, False, False)
            Me.DataGridView2.SelectionMode = DataGridViewSelectionMode.CellSelect
            Me.DataGridView2.AutoResizeColumns(System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells)
            If Me.fLoadBool = True Then
                SetButton(ButtonType.Browse, False)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK)
        Finally
            Me.Cursor = Cursors.Default
        End Try
    End Sub



    Public Function ReturnCaption(ByVal Index) As Hashtable

        Dim Hash As New Hashtable
        Select Case Index
            Case 1
                Hash.Add("ListenPsn", "姓名")
                Hash.Add("fTotals", "投訴總數")
                Hash.Add("fDays", "總天數")
                Hash.Add("fAverage", "平均每天處理投訴總數")
        End Select
        Return Hash
    End Function

#End Region

#Region "SetEnabled"

    ''' <summary>
    ''' 設置按鈕狀態


    ''' </summary>
    ''' <param name="type">操作類型</param>
    ''' <param name="IsFind">是否是流覽</param>
    ''' <remarks></remarks>
    Private Sub SetButton(ByVal type As ButtonType, ByVal IsFind As Boolean)
        Try

            Select Case type
                Case ButtonType.Add
                    With MainForm
                        .ToolStripAdd.Enabled = False
                        .ToolStripEdit.Enabled = False
                        .ToolStripDelete.Enabled = False
                        .ToolStripSave.Enabled = True
                        .ToolStripCancel.Enabled = True
                        .ToolStripFirst.Enabled = False
                        .ToolStripLast.Enabled = False
                        .ToolStripNext.Enabled = False
                        .ToolStripPrivous.Enabled = False
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

                Case ButtonType.Browse
                    With MainForm
                        .ToolStripAdd.Enabled = False
                        .ToolStripEdit.Enabled = IsFind
                        .ToolStripDelete.Enabled = IsFind
                        .ToolStripSave.Enabled = False
                        .ToolStripCancel.Enabled = False
                        .ToolStripFirst.Enabled = IsFind
                        .ToolStripLast.Enabled = IsFind
                        .ToolStripNext.Enabled = IsFind
                        .ToolStripPrivous.Enabled = IsFind
                        .ToolStripExport.Enabled = True
                        .ToolStripImport.Enabled = False
                        .ToolStripPrint.Enabled = True
                        .ToolStripFilter.Enabled = True
                        .ToolStripCheck.Enabled = False
                        .ToolStripComplete.Enabled = False
                        .ToolStripUnCheck.Enabled = False
                    End With

                Case ButtonType.Cancel
                    With MainForm
                        .ToolStripAdd.Enabled = True
                        .ToolStripEdit.Enabled = IsFind
                        .ToolStripDelete.Enabled = IsFind
                        .ToolStripSave.Enabled = False
                        .ToolStripCancel.Enabled = False
                        .ToolStripFirst.Enabled = IsFind
                        .ToolStripLast.Enabled = IsFind
                        .ToolStripNext.Enabled = IsFind
                        .ToolStripPrivous.Enabled = IsFind
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

                Case ButtonType.Edit
                    With MainForm
                        .ToolStripAdd.Enabled = False
                        .ToolStripEdit.Enabled = False
                        .ToolStripDelete.Enabled = False
                        .ToolStripSave.Enabled = True
                        .ToolStripCancel.Enabled = True
                        .ToolStripFirst.Enabled = False
                        .ToolStripLast.Enabled = False
                        .ToolStripNext.Enabled = False
                        .ToolStripPrivous.Enabled = False
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

            End Select
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

#End Region

#End Region

#Region "Events"

    Private Sub Frm_CSPrintMonthCount_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.fLoadBool = False
        Me.dtp_start_1.Text = Format(CType(ServerDate, Date), "yyyy-MM-01")
        Me.dtp_end_1.Text = ServerDate
        Me.dtp_start_2.Text = Format(CType(ServerDate, Date), "yyyy-MM-01")
        Me.dtp_end_2.Text = ServerDate
        FillComboxForEnum(Me.cbxfLevel, GetType(ModEnum.ComplainLevel))
        Me.cbxfLevel.Text = ModEnum.ComplainLevel.嚴重.ToString()
        Me.fLoadBool = True
    End Sub

    Private Sub btnFind_1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind_1.Click
        ExecFind()
    End Sub

    Private Sub btnFind_2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind_2.Click
        ExecFind_2()
    End Sub

    Private Sub DataGridView1_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DataGridView1.RowPostPaint
        Try
            Dim Grid As DataGridView = DirectCast(sender, DataGridView)
            Dim SBrush As New SolidBrush(Grid.RowHeadersDefaultCellStyle.ForeColor)
            e.Graphics.DrawString(Convert.ToString(e.RowIndex + 1, System.Globalization.CultureInfo.CurrentCulture), e.InheritedRowStyle.Font, SBrush, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4)
            SBrush = Nothing
        Catch ex As Exception
            MessageBox.Show("DGV_Master_RowPostPaint:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub DataGridView2_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DataGridView2.RowPostPaint
        Try
            Dim Grid As DataGridView = DirectCast(sender, DataGridView)
            Dim SBrush As New SolidBrush(Grid.RowHeadersDefaultCellStyle.ForeColor)
            e.Graphics.DrawString(Convert.ToString(e.RowIndex + 1, System.Globalization.CultureInfo.CurrentCulture), e.InheritedRowStyle.Font, SBrush, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4)
            SBrush = Nothing
        Catch ex As Exception
            MessageBox.Show("DGV_Master_RowPostPaint:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

#End Region

#Region "Interface"

    Public Function ActionAdd() As Boolean Implements IDServer.IDServer.IDServer.ActionAdd
        SelectObject.ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
    End Function

    Public Function ActionCancel() As Boolean Implements IDServer.IDServer.IDServer.ActionCancel

    End Function

    Public Function ActionDelete() As Boolean Implements IDServer.IDServer.IDServer.ActionDelete

    End Function

    Public Function ActionEdit() As Boolean Implements IDServer.IDServer.IDServer.ActionEdit

    End Function

    Public Function ActionExport() As Boolean Implements IDServer.IDServer.IDServer.ActionExport
        Try
            Me.Cursor = Cursors.WaitCursor

            Select Case Me.TabControl1.SelectedIndex
                Case 0
                    If Me.DataGridView1.Rows.Count <= 0 Then
                        Return True
                    End If

                    Dim Grid As DataGridView = Me.DataGridView1
                    Dim ds As New DataSet
                    Dim Dtab As DataTable
                    Dtab = DirectCast(Grid.DataSource, DataTable).Copy
                    Dtab.TableName = "Receive"
                    Dim Hash As New Hashtable
                    RemoveHideColumn(Grid, Dtab)
                    Dim export As New ExcelExport()
                    Dim dw As Int16 = DatePart(DateInterval.Month, Me.dtp_end_1.Value)
                    export.ExprotToExcel_PrintMonthCount(Dtab, dw.ToString & "月每日來電統計表")

                Case 1
                    If Me.DataGridView2.Rows.Count <= 0 Then
                        Return True
                    End If

                    Dim Grid As DataGridView = Me.DataGridView2
                    Dim ds As New DataSet
                    Dim Dtab As DataTable
                    Dtab = DirectCast(Grid.DataSource, DataTable).Copy
                    Dtab.TableName = "Receive"
                    Dim Hash As New Hashtable
                    RemoveHideColumn(Grid, Dtab)
                    Dim export As New ExcelExport()
                    Dim dw As Int16 = DatePart(DateInterval.Month, Me.dtp_end_1.Value)
                    export.ExprotToExcel(Dtab, dw.ToString & "月每日投訴統計表")

            End Select
          
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            Me.Cursor = Cursors.Default
        End Try

    End Function

    Public Function ActionFilter() As Boolean Implements IDServer.IDServer.IDServer.ActionFilter

    End Function

    Public Function ActionGoFirst() As Boolean Implements IDServer.IDServer.IDServer.ActionGoFirst

    End Function

    Public Function ActionGoLast() As Boolean Implements IDServer.IDServer.IDServer.ActionGoLast

    End Function

    Public Function ActionGoNext() As Boolean Implements IDServer.IDServer.IDServer.ActionGoNext

    End Function

    Public Function ActionGoPrevious() As Boolean Implements IDServer.IDServer.IDServer.ActionGoPrevious

    End Function

    Public Function ActionImport() As Boolean Implements IDServer.IDServer.IDServer.ActionImport

    End Function

    Public Function ActionInitialize() As Boolean Implements IDServer.IDServer.IDServer.ActionInitialize

    End Function

    Public Function ActionPrint() As Boolean Implements IDServer.IDServer.IDServer.ActionPrint

    End Function

    Public Function ActionSave() As Boolean Implements IDServer.IDServer.IDServer.ActionSave

    End Function

    Public Function ActionSelect() As Boolean Implements IDServer.IDServer.IDServer.ActionSelect

    End Function

#End Region


End Class
